package nemeanshop;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ragnar
 */
public class Order
{
    private Cashier cashier;
    private double price;
    private int saleID;
    private ArrayList<Product> basket;
    
    public Order(Cashier cashier)
    {
        
        this.cashier = cashier;
        this.basket = new ArrayList<Product>();
        updatePrice();
    }

    public int getSaleID() {
        return saleID;
    }
    
    
    
    public Cashier getCashier()
    {
        return this.cashier;
    }
    
    public Product addProduct(Product product)
    {
        int index = this.hasProduct(product.getBarcode());
        if (index != -1)
        {
            Product p = this.basket.get(index);
            try {
                p.incrementQuantity(1);
            } catch (NemeanShopException ex) {
                ex.getMsg();
            }
            return p;
        }
        else
        {
            
            if(product.getQty()>0){
            this.basket.add(new Product(product.getBarcode(), product.getProductName(), product.getSellPrice(), product.getQty()));
            return product;
            }
            else{
                try {
                    throw new NemeanShopException("Ikke flere på lager");
                
                } catch (NemeanShopException ex) {
                    ex.getMsg();
                    return null;
                }
                
            }
        }       
    }
    
    public void print()
    {
        System.out.println("<------ Handlekurv ------->");
        System.out.println("---------------------------");
        for (Product p : this.basket)
        {
            System.out.println("Vare - " + p.getProductName() + " - x" + p.getTotal());
        }
    }
    
    public int removeProduct(String bcode)
    {
        int index = this.hasProduct(bcode);
        this.basket.remove(index);
        return index;
    }
    
    public int hasProduct(String bcode)
    {
        for (Product p : this.basket)
        {
            if (p.getBarcode().equals(bcode))
            {
                return this.basket.indexOf(p);
            }
        }
        return -1;
    }

    public ArrayList<Product> getBasket() {
        return basket;
    }
    
    
    private void updatePrice(){
        double p = 0;
        
        for(int i = 0; i <basket.size(); i++){
            
            p+= basket.get(i).getSellPrice();
        }
        this.price = p;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    
    public double getTotal()
    {
        double total = 0;
        for (Product p : this.basket)
        {
            total += (p.getTotal()*p.getSellPrice());
        }
        return total;
            
    }
    
    public void reset()
    {
        this.basket = new ArrayList<Product>();
    }

        
}
